package com.hg.controller;

import com.hg.pojo.Content;
import com.hg.pojo.ContentCategory;
import com.hg.service.IContentService;
import com.hg.util.PageResult;
import com.hg.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Baomidou
 * @since 2025-07-31
 */
@RestController
@RequestMapping("/backend/content")
public class ContentController {
    @Autowired
    private IContentService contentService;

    @RequestMapping("/selectContentCategoryByParentId")
    public Result selectContentCategoryByParentId(@RequestParam(defaultValue = "0") Long id){
        try {
            List<ContentCategory> contentCategoryList = contentService.selectContentCategoryByParentId(id);
            return Result.ok(contentCategoryList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    @RequestMapping("/insertContentCategory")
    public Result insertContentCategory(Long parentId,String name){
        try {
            contentService.insertContentCategory(parentId,name);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        }
    }

    @RequestMapping("/deleteContentCategoryById")
    public Result deleteContentCategoryById(Long categoryId){
        try {
            contentService.deleteContentCategoryById(categoryId);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    @RequestMapping("/updateContentCategory")
    public Result updateContentCategory(Long id,String name){
        try {
            contentService.updateContentCategory(id,name);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }
    }

    @RequestMapping("/selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(Long categoryId,
                                                 @RequestParam(defaultValue = "1") Integer page,
                                                 @RequestParam(defaultValue = "5") Integer rows){
        try {
           PageResult pageResult = contentService.selectTbContentAllByCategoryId(categoryId,page,rows);
            return Result.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }
    @RequestMapping("/insertTbContent")
    public Result insertTbContent(Content content){
        try {
            contentService.insertTbContent(content);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        }
    }

    @RequestMapping("/deleteContentByIds")
    public Result deleteContentByIds(Long ids){
        try {
            contentService.deleteContentByIds(ids);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        }
    }
}
